package com.hubu.blog.utils.gen;

import com.mybatisflex.codegen.Generator;
import com.mybatisflex.codegen.config.EntityConfig;
import com.mybatisflex.codegen.config.GlobalConfig;
import com.zaxxer.hikari.HikariDataSource;

public class MybatisFlexGenerator {


    public static void main(String[] args) {
        //配置数据源
        HikariDataSource dataSource = new HikariDataSource();
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/db_blog?characterEncoding=utf-8");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        //创建配置内容，两种风格都可以。
        GlobalConfig globalConfig = createGlobalConfigUseStyle();

        //通过 datasource 和 globalConfig 创建代码生成器
        Generator generator = new Generator(dataSource, globalConfig);

        //生成代码
        generator.generate();
    }

    public static GlobalConfig createGlobalConfigUseStyle() {
        //创建配置内容
        GlobalConfig globalConfig = new GlobalConfig();
        // 设置使用 @Resource 注解
        //1.设置javadoc
        globalConfig.getJavadocConfig()
                .setAuthor("331126135@qq.com");

        //2.设置根包
        globalConfig.getPackageConfig()
                .setBasePackage("com.hubu.blog")
                .setMapperXmlPath("src/main/resources/mapper");  // 设置XML文件生成路径

        //3.设置根包
        globalConfig.getStrategyConfig()
                .setTablePrefix("blog_")
                .setLogicDeleteColumn("is_deleted").setGenerateSchema("db_blog");

        //设置生成 entity 并启用 Lombok
        globalConfig.enableEntity()
                .setWithLombok(true)

                .setWithSwagger(true)
                .setSwaggerVersion(EntityConfig.SwaggerVersion.DOC)
                .setOverwriteEnable(true)
                .setJdkVersion(17);

        globalConfig.enableMapper();

        globalConfig.enableController();

        globalConfig.enableMapperXml();
        globalConfig.enableService();
//        globalConfig.enablePackageInfo();
        globalConfig.enableServiceImpl();
        globalConfig.enableTableDef();


        return globalConfig;
    }
}
